<?php

namespace App\User\Models;


use App\Models\BaseModel;
use Illuminate\Support\Facades\Log;


class UserBindAppModel extends BaseModel
{
    protected $table = 'user_bind_app';

    public function getUserBindApp($userId, $appId = null)
    {
        $where = [
            'a.user_id' => $userId,
            'a.status' => 0
        ];
        if(isset($appId)) {
            $where['app_id'] = $appId;
        }
        $data = $this->db('a')
            ->leftJoin('sys_app as b', 'a.app_id', '=', 'b.id')
            ->where($where)->selectRaw('b.id,b.short_name,b.name,b.code')
            ->get()->toArray();
        if (!empty($data)) {
            foreach ($data as &$datum) {
                $datum = (array)$datum;
            }
        }
        return $data;
    }

    public function saveUserBindApp($userId, $appId)
    {
        return $this->db()
            ->insertGetId([
                'user_id' => $userId,
                'app_id' => $appId,
                'status' => 0
            ]);
    }

    public function delUserBindApp($userId, $appId = null)
    {
        $where = [
            'user_id' => $userId,
            'status' => 0
        ];
        if (isset($appId)) {
            $where['app_id'] = $appId;
        }
        return $this->db()
            ->where($where)
            ->update([
                'status' => 2
            ]);
    }

}
